package com.eagle.coders.scheduler.crond;

/**
 * 
 * @author    anees    
 * @version   1.0 
 * 
 * NEED TO CONVERT TO DROOLS
 * 
 *
 */
public class CronExpressionCreatorUtils {

//	private static CronObject cronObject = null;
	
	private static String minuteExpression = null;
	private static String hourExpression = null;
	private static String cronExpression = null;
    private static String dayOfMonthExpression = null;
    private static String dayOfWeekExpression = null;
    private static String monthExpression = null;

    
	
	/**
	 * 
	 * @param cronObject
	 */
	public CronExpressionCreatorUtils(){
	}
	
	/**
	 * 
	 * @return
	 */
	public String createCronExpression(CronObject cronObject){
		String dayOfWeek= cronObject.getDayOfWeek();
        String dayOfMonth = cronObject.getDayOfMonth();
		String hour = cronObject.getHours();
        String month = cronObject.getMonth();
		String mins = cronObject.getMinutes();

		if(dayOfWeek.equals(CronObject.ALL_DAYS_OF_WEEK)&& dayOfMonth.equals(CronObject.ALL_DAYS_OF_MONTH)){
            dayOfMonthExpression = "?";
            dayOfWeekExpression = "*";
        }
        
        else if(dayOfWeek.equals(CronObject.ALL_DAYS_OF_WEEK)){
            dayOfWeekExpression = "?";
        }
        
        else if(dayOfMonth.equals(CronObject.ALL_DAYS_OF_MONTH)){
            dayOfMonthExpression = "?";
        }
        
        if(mins.equals(CronObject.ALL_MINUTES)){
            minuteExpression = "*";
        }
			
		if(month.equals(CronObject.ALL_MONTHS)){
			monthExpression = "*";
		}
        
        if(hour.equals(CronObject.ALL_HOURS)){
            hourExpression = "*";
        }
        
       		
		String timeIntervalHour = cronObject.getTimeIntervalHour();
		String timeIntervalMins = cronObject.getTimeIntervalMinutes();
		String timeFrequencyHour = cronObject.getTimeFrequencyHour();
		String timeFrequencyMins = cronObject.getTimeFrequencyMinutes();
        String monthInterval = cronObject.getMonthInterval();
        String dayOfMonthInterval = cronObject.getDaysOfMonthInterval();
        String dayOfWeekInterval = cronObject.getDayOfWeekInterval();
        
        

 // Executes when interval is defined for hour OR minute
//		if(null!=timeIntervalMins || null!= timeFrequencyMins){
//        
//            if(null!=timeIntervalMins && !mins.equals(CronObject.ALL_MINUTES)&& null==timeFrequencyMins){     
//
//                int minutes = Integer.parseInt(mins);
//                int minInterval = Integer.parseInt(timeIntervalMins);
//                if((minutes > 0) && (minutes < 59) && (minInterval > 0) && (minInterval <=59) && (minInterval > minutes)){
//                    minuteExpression = minutes + "-" + minInterval;
//                }
//            }
//            else if(null != timeFrequencyMins && !mins.equals(CronObject.ALL_MINUTES)&& null == timeIntervalMins){
//                minuteExpression = mins+"/"+timeFrequencyMins;
//                
//            }
//            else if(!mins.equals(CronObject.ALL_MINUTES)){
//                minuteExpression = mins;
//            }
//        }
        if(!mins.equals(CronObject.ALL_MINUTES)){
            minuteExpression = mins;
            if(!timeFrequencyMins.equals("")){
                minuteExpression = minuteExpression+"/"+timeFrequencyMins;
            }
            if(!timeIntervalMins.equals("")){
                minuteExpression = minuteExpression+"-"+timeIntervalMins;
            }
//            else{
//                minuteExpression = mins;
//            }
           
        }
		
//  Executes When frequency is defined for hour OR minute
//        if(null!= timeIntervalHour || null!=timeFrequencyHour){
//            if(null!=timeIntervalHour && !hour.equals(CronObject.ALL_HOURS)&& null==timeFrequencyHour){
//                
//                int hours = Integer.parseInt(hour);
//                int hourInterval = Integer.parseInt(timeIntervalHour);
//                System.out.println("Hour>>>>>>>>>>" + hour + "\nHour Interval>>>>>>>>>>" + hourInterval);
//                if((hours > 0) && (hours < 23) && (hourInterval > 0) && (hourInterval <=23) && (hourInterval > hours)){
//                    System.out.println("Hour>>>>>>>>>>" + hour + "\nHour Interval>>>>>>>>>>" + hourInterval);
//                    hourExpression = hours + "-" + hourInterval;
//                }
//            }
//            else if(null != timeFrequencyHour && !hour.equals(CronObject.ALL_HOURS)&& null==timeIntervalHour){
//                hourExpression = hour+"/"+timeFrequencyHour;
//            }
//            else if(!hour.equals(CronObject.ALL_HOURS)){
//                hourExpression = hour;
//            }
//        }
        if(!hour.equals(CronObject.ALL_HOURS)){
            hourExpression = hour;
            if(!timeFrequencyHour.equals("")){
                hourExpression = hourExpression+"/"+timeFrequencyHour;
            }
            if(!timeIntervalHour.equals("")){
                hourExpression = hourExpression+"-"+timeIntervalHour;
            }
//            else{
//                minuteExpression = mins;
//            }
           
        }
        
        if(null != dayOfWeekInterval && !dayOfWeek.equals(CronObject.ALL_DAYS_OF_WEEK)){
            dayOfWeekExpression = dayOfWeek+"-"+dayOfWeekInterval;
        }
        else if(!dayOfWeek.equals(CronObject.ALL_DAYS_OF_WEEK)){
            dayOfWeekExpression = dayOfWeek;
        }
        
        if(null != dayOfMonthInterval && !dayOfMonth.equals(CronObject.ALL_DAYS_OF_MONTH)){
            dayOfMonthExpression = dayOfMonth+"-"+dayOfMonthInterval;
        }
        else if(!dayOfMonth.equals(CronObject.ALL_DAYS_OF_MONTH)){
            dayOfMonthExpression = dayOfMonth;
        }
        
        if(null!= monthInterval && !month.equals(CronObject.ALL_MONTHS)){
            monthExpression = month+"-"+monthInterval;
        }
        else if(!month.equals(CronObject.ALL_MONTHS)){
            monthExpression = month;
        }
		
		cronExpression = "0 "+minuteExpression+" "+hourExpression+" "+ dayOfMonthExpression+" "+ 
                               monthExpression+" "+dayOfWeekExpression;


		
		return cronExpression;
	}
    
    public CronObject createCronObject(String cronExpression){
        
//          CronExpressionCreatorUtils cronExpressionUtils = new CronExpressionCreatorUtils();
          CronObject cronObject = new CronObject();
//        String dayOfWeek= cronObject.getDayOfWeek();
//        String dayOfMonth = cronObject.getDayOfMonth();
//        String hour = cronObject.getHours();
//        String month = cronObject.getMonth();
//        String mins = cronObject.getMinutes();
        
        String[] splitExpression = cronExpression.split(" ");

        for(int i=0;i<splitExpression.length;i++){
            System.out.println("Expression " + (i+1) +": " + splitExpression[i]);
        }
      
      String mins = splitExpression[1];
      String hour = splitExpression[2];
      String dayOfMonth = splitExpression[3];
      String month = splitExpression[4];
      String dayOfWeek= splitExpression[5];
      
      setMinutes(mins, cronObject);
      setHours(hour, cronObject);
      setDayofMonth(dayOfMonth, cronObject);
      setMonth(month, cronObject);
      setDayofWeek(dayOfWeek, cronObject);
      
      return cronObject;
    }
    
    
    
    public static void main(String[] args){
        CronExpressionCreatorUtils cronExpressionUtils = new CronExpressionCreatorUtils();
        CronObject cronObject = new CronObject();
//        cronObject.setDayOfMonth("19");
//        cronObject.setDaysOfMonthInterval("23");
        cronObject.setDayOfMonth(CronObject.ALL_DAYS_OF_MONTH);
        cronObject.setDayOfWeek(CronObject.ALL_DAYS_OF_WEEK);
//        cronObject.setDayOfWeek(CronObject.MONDAY);
//        cronObject.setDayOfWeekInterval(CronObject.THURSDAY);
        cronObject.setMonth(CronObject.JANUARY);
        cronObject.setMonthInterval(CronObject.AUGUST);
        cronObject.setHours("22");
        cronObject.setTimeIntervalHour("23");
        cronObject.setTimeFrequencyHour("2");
//        cronObject.setMinutes(CronObject.ALL_MINUTES);
        cronObject.setMinutes("10");
        cronObject.setTimeIntervalMinutes("20");
        cronObject.setTimeFrequencyMinutes("30");
//        cronObject.setTimeIntervalMinutes("");
//        cronObject.setTimeFrequencyHour("");
//        cronObject.setTimeFrequencyMinutes("");
        String cronExpression = cronExpressionUtils.createCronExpression(cronObject);
        System.out.println(cronExpression);
        cronObject = cronExpressionUtils.createCronObject("* 10/20 12-14 31 * ?");
        
        System.out.println("Minutes " + cronObject.getMinutes());
        System.out.println("MinuteFrequency " + cronObject.getTimeFrequencyMinutes());
        System.out.println("MinuteInterval " + cronObject.getTimeIntervalMinutes());
        System.out.println("Hours " + cronObject.getHours());
        System.out.println("HourFrequency " + cronObject.getTimeFrequencyHour());
        System.out.println("HourInterval " + cronObject.getTimeIntervalHour());
        System.out.println("DayOfMonth " + cronObject.getDayOfMonth());
        System.out.println("Month " + cronObject.getMonth());
        System.out.println("MonthInterval " + cronObject.getMonthInterval());
        System.out.println("DayOfWeek " + cronObject.getDayOfWeek());
        System.out.println("DayOfWeekInterval " + cronObject.getDayOfWeekInterval());
        
       
    }
    
    private CronObject setMinutes(String min, CronObject cronObject){
        
        if(min.equals("*")){
            cronObject.setMinutes(CronObject.ALL_MINUTES);
        }
        else{
            if(min.contains("/")&& min.contains("-")){
                cronObject.setMinutes(min.split("/")[0]);
                cronObject.setTimeFrequencyMinutes(min.split("/")[1].split("-")[0]);
                cronObject.setTimeIntervalMinutes(min.split("-")[1]);
            }
            else if(min.contains("/")){
                cronObject.setMinutes(min.split("/")[0]);
                cronObject.setTimeFrequencyMinutes(min.split("/")[1]);
            }
            else if(min.contains("-")){
                cronObject.setMinutes(min.split("-")[0]);
                cronObject.setTimeIntervalMinutes(min.split("-")[1]);
            }
            else{
                cronObject.setMinutes(min);
            }
          
        }
        return cronObject;
    }
    
    private CronObject setHours(String hour, CronObject cronObject) {
        if (hour.equals("*")) {
            cronObject.setHours(CronObject.ALL_HOURS);
        }
        else {
            if (hour.contains("/") && hour.contains("-")) {
                cronObject.setHours(hour.split("/")[0]);
                cronObject.setTimeFrequencyHour(hour.split("/")[1].split("-")[0]);
                cronObject.setTimeIntervalHour(hour.split("-")[1]);
            } else if (hour.contains("/")) {
                cronObject.setHours(hour.split("/")[0]);
                cronObject.setTimeFrequencyHour(hour.split("/")[1]);
            } else if (hour.contains("-")) {
                cronObject.setHours(hour.split("-")[0]);
                cronObject.setTimeIntervalHour(hour.split("-")[1]);
            } else {
                cronObject.setHours(hour);
            }

        }
        return cronObject;
    }
    
    private CronObject setDayofMonth(String dayOfMonth, CronObject cronObject){
        if(dayOfMonth.equals("?")|| dayOfMonth.equals("*")){
            cronObject.setDayOfMonth(CronObject.ALL_DAYS_OF_MONTH);
        }
        else{
            cronObject.setDayOfMonth(dayOfMonth);
        }
        return cronObject;
    }
    
    private CronObject setMonth(String month, CronObject cronObject){
        if(month.equals("*")){
            cronObject.setMonth(CronObject.ALL_MONTHS);
        }
        else{
            if(month.contains("-")){
                cronObject.setMonth(month.split("-")[0]);
                cronObject.setMonthInterval(month.split("-")[1]);
            }
            else{
                cronObject.setMonth(month);
            }
        }
        return cronObject;
    }
    
    private CronObject setDayofWeek(String dayOfWeek, CronObject cronObject){
        if(dayOfWeek.equals("*")||dayOfWeek.equals("?")){
            cronObject.setDayOfWeek(CronObject.ALL_MONTHS);
        }
        else{
            if(dayOfWeek.contains("-")){
                cronObject.setDayOfWeek(dayOfWeek.split("-")[0]);
                cronObject.setDayOfWeekInterval(dayOfWeek.split("-")[1]);
            }
            else{
                cronObject.setDayOfWeek(dayOfWeek);
            }
        }
        return cronObject;
    }
}
